package com.gotoxoyo.demo.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.sql.DataSource;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.gotoxoyo.demo.dao.CustomerDao;
import com.gotoxoyo.demo.dao.JdbcCustomerDao;
import com.gotoxoyo.demo.pojo.Customer;
import com.gotoxoyo.demo.service.LoginService;

@Controller
public class LoginController {

	@Resource
	LoginService service;
	
	@Resource
	HttpServletRequest request;
	
	@Resource
	private CustomerDao customerDao;
	
	public CustomerDao getCustomerDao() {
		return customerDao;
	}

	public void setCustomerDao(CustomerDao customerDao) {
		this.customerDao = customerDao;
	}
	
	@Resource
	private Customer customer;
	

	public Customer getCustomer() {
		return customer;
	}

	public void setCustomer(Customer customer) {
		this.customer = customer;
	}

	@RequestMapping("index")
	public ModelAndView toLoginPage() {
/*		ApplicationContext context =
	    		new ClassPathXmlApplicationContext("spring-mvc.xml");

	        CustomerDao customerDao = (CustomerDao) context.getBean("customerDAO");
	        Customer customer = new Customer(1, "mkyong",28);
	        customerDao.insert(customer);
	        

	        Customer customer1 = customerDao.findByCustomerId(1);
	        System.out.println(customer1);*/
		
		
		
		return new ModelAndView("WEB-INF/jsp/login.jsp");
	}
	
	@RequestMapping("login")
	public ModelAndView doLogin() {
/*		ApplicationContext context =
	    		new ClassPathXmlApplicationContext("spring-mvc.xml");

	        CustomerDao customerDao = (CustomerDao) context.getBean("customerDAO");
	        Customer customer = new Customer(1, "mkyong",28);
	        customerDao.insert(customer);
	        

	        Customer customer1 = customerDao.findByCustomerId(1);
	        System.out.println(customer1);
		DriverManagerDataSource dataSource = new DriverManagerDataSource();
		dataSource.setUrl("jdbc:mysql://localhost:3306/gotoxoyo");
		dataSource.setUsername("root");
		dataSource.setPassword("go111112");
		dataSource.setDriverClassName("com.mysql.jdbc.Driver");
		
		ApplicationContext context =
	    		new ClassPathXmlApplicationContext("applicationContext.xml");

        CustomerDao customerDao = (CustomerDao) context.getBean("customerDao");
		Customer customer = new Customer(1, "mkyong",28);
 */
		customerDao.insert(customer);
		
		
		Customer customer1 = customerDao.findByCustomerId(1);
		System.out.println(customer1);
		
		String loginPageUrl = "WEB-INF/jsp/login.jsp";
		String successPageUrl = "WEB-INF/jsp/success.jsp";
		
		String uname = request.getParameter("uname");
		String upasswd = request.getParameter("upasswd");
		
		return service.doLogin(loginPageUrl, successPageUrl, uname, upasswd);
		
	}
}
